package app.modules.cloudpivot.authentication;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import com.authine.cloudpivot.engine.spi.auth.RestAuthenticationService;
import com.authine.cloudpivot.engine.spi.metadata.AdapterConfigParam;
import com.authine.cloudpivot.engine.spi.metadata.AdapterConfigParam.Auth;
import com.authine.cloudpivot.engine.spi.metadata.RestAuthenticationSetting;

/**
 * Hmac鉴权处理
 *
 * @author LiuGangQiang Create in 2024/08/02
 */
@Service
public class HmacAuthenticationServiceImpl implements RestAuthenticationService {

    @Override
    public Auth getAuthConfig() {
        /* 定义鉴权信息 */
        List<AdapterConfigParam.Param> params = new ArrayList<>();
        params.add(new AdapterConfigParam.Param("ak", "AK", "Hmac系统的AK", true));
        params.add(new AdapterConfigParam.Param("sk", "SK", "Hmac系统的AK", true));
        return new AdapterConfigParam.Auth("hmac_auth", "Hmac系统鉴权", "Hmac系统鉴权 通过AK/SK进行签名并追加到请求头实现", params);
    }

    @Override
    public HttpRequest getAuthenticatedRequest(RestAuthenticationSetting setting, HttpRequest request) {
        /* 获取鉴权设置的参数 */
        String ak = setting.getAuthParams().get("ak");
        String sk = setting.getAuthParams().get("sk");
        request.getHeaders().add("ak", ak);
        request.getHeaders().add("sk", sk);
        return request;
    }
}
